<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Worker 类</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="thread.start.html">« Thread::start</a></li>
      <li style="float: right;"><a href="worker.collect.html">Worker::collect »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="book.pthreads.html">pthreads</a></li>
    <li>Worker 类</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="class.worker" class="reference">

 <h1 class="title">Worker 类</h1>
 

 <div class="partintro"><p class="verinfo">(PECL pthreads &gt;= 2.0.0)</p>


  <div class="section" id="worker.intro">
   <h2 class="title">简介</h2>
   <p class="para">
    Worker 是一个具有持久化上下文的线程对象，通常用来在多个线程中使用。
   </p>
   <p class="para">
    当一个 Worker 对象开始之后，会执行它的 run 方法，但是即使 run 方法执行完毕，线程本身也不会消亡，除非遇到以下情况：
   </p>
   <ul class="itemizedlist">
    <li class="listitem">
    <p class="para">Worker 对象超出作用范围（没有指向它的引用了）</p>
    </li>
    <li class="listitem">
    <p class="para">代码调用了 Worker 对象的 shutdown 方法</p>
    </li>
    <li class="listitem">
    <p class="para">整个脚本终止了</p>
    </li>
   </ul>
   <p class="para">
    这意味着程序员可以在程序执行过程中重用这个线程上下文：
    在 Worker 对象的栈中添加对象会激活 Worker 对象执行被加入对象的 run 方法。
   </p>
  </div>


  <div class="section" id="worker.synopsis">
   <h2 class="title">类摘要</h2>


   <div class="classsynopsis">
    <div class="ooclass"></div>


    <div class="classsynopsisinfo">
     <span class="ooclass">
      <span class="modifier">class</span> <strong class="classname">Worker</strong>
     </span>
     
     <span class="ooclass">
      <span class="modifier">extends</span>
       <a href="class.thread.html" class="classname">Thread</a>
     </span>
     
     <span class="oointerface"><span class="modifier">implements</span> 
       <a href="class.traversable.html" class="interfacename">Traversable</a></span><span class="oointerface">,  <a href="class.countable.html" class="interfacename">Countable</a></span><span class="oointerface">,  <a href="class.arrayaccess.html" class="interfacename">ArrayAccess</a></span> {</div>

    
    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 方法 */</div>
    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="worker.collect.html" class="methodname">collect</a></span>(<span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type Callable">Callable</a></span> <code class="parameter">$collector</code><span class="initializer"> = ?</span></span>): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="worker.getstacked.html" class="methodname">getStacked</a></span>(): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="worker.isshutdown.html" class="methodname">isShutdown</a></span>(): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="worker.shutdown.html" class="methodname">shutdown</a></span>(): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="worker.stack.html" class="methodname">stack</a></span>(<span class="methodparam"><span class="type"><a href="class.threaded.html" class="type Threaded">Threaded</a></span> <code class="parameter reference">&$work</code></span>): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="worker.unstack.html" class="methodname">unstack</a></span>(): <span class="type">int</span></div>

    
    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 继承的方法 */</div>
    <div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="thread.getcreatorid.html" class="methodname">Thread::getCreatorId</a></span>(): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><a href="thread.getcurrentthread.html" class="methodname">Thread::getCurrentThread</a></span>(): <span class="type"><a href="class.thread.html" class="type Thread">Thread</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><a href="thread.getcurrentthreadid.html" class="methodname">Thread::getCurrentThreadId</a></span>(): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="thread.getthreadid.html" class="methodname">Thread::getThreadId</a></span>(): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span><span class="methodname"><a href="thread.isjoined.html" class="methodname">Thread::isJoined</a></span>(): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span><span class="methodname"><a href="thread.isstarted.html" class="methodname">Thread::isStarted</a></span>(): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span><span class="methodname"><a href="thread.join.html" class="methodname">Thread::join</a></span>(): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="thread.start.html" class="methodname">Thread::start</a></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$options</code><span class="initializer"> = ?</span></span>): <span class="type">bool</span></div>


   }</div>


  </div>

 </div>

 






















































<h2>目录</h2><ul class="chunklist chunklist_reference"><li><a href="worker.collect.html">Worker::collect</a> — Collect references to completed tasks</li><li><a href="worker.getstacked.html">Worker::getStacked</a> — 获取剩余的栈大小</li><li><a href="worker.isshutdown.html">Worker::isShutdown</a> — 状态检测</li><li><a href="worker.shutdown.html">Worker::shutdown</a> — 关闭 Worker</li><li><a href="worker.stack.html">Worker::stack</a> — 将要执行的任务入栈</li><li><a href="worker.unstack.html">Worker::unstack</a> — 将要执行的任务出栈</li></ul>
</div>
</div></div></body></html>